package com.ifudata.dvp.app.service.busi.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ifudata.dvp.app.dao.mapper.base.bo.AppMenu;
import com.ifudata.dvp.app.service.atom.interfaces.IAppMenuAtomSV;
import com.ifudata.dvp.app.service.busi.interfaces.IAppDataBusiSV;
import com.ifudata.dvp.app.util.GetDataUtil;

/**
 * 
 * @description
 * @author yuhuan 2018年10月26日
 * @Modifier
 */
@Service
public class AppDataBusiSVImpl implements IAppDataBusiSV {

	@Autowired
	private IAppMenuAtomSV appMenuAtomSV;
	
	@Override
	public String getReportDetail(Long parentId) {
		JSONObject re = new JSONObject();
		
		AppMenu dateMenu = null, selectTopMenu = null;
		List<AppMenu> conditionMenuList = new ArrayList<AppMenu>(), 
				otherMenuList = new ArrayList<AppMenu>();
		
		List<AppMenu> list = appMenuAtomSV.queryAppMenus(parentId);
		for( AppMenu menu : list ){
			switch( menu.getConfigType() ) {
				case "DATE" : {
					dateMenu = menu;
					break;
				}
				case "SELECT_TOP_LIST" : {
					selectTopMenu = menu;
					break;
				}
				case "CONDITION" : {
					conditionMenuList.add(menu);
					break;
				}
				case "CONDITION_SQL" : {
					conditionMenuList.add(menu);
					break;
				}
				default :{
					otherMenuList.add(menu);
					break;
				}
			}
		}
		
		// 先获取时间 和 查询条件
		Map<String, String> param = new HashMap<String, String>();
		if( dateMenu!=null ){
			JSONObject o = getDate(dateMenu);
			re.put("DATE", o);
			param.put("${"+dateMenu.getConfigKey()+"}", o.getString("currentDate"));
		}
		if( conditionMenuList.size()>0 ){
			JSONArray array = new JSONArray();
			for( AppMenu menu : conditionMenuList ){
				JSONObject o;
				if( menu.getConfigType().equals("CONDITION_SQL") ){
					o = getConditionSql(menu);
				}else{
					o = getCondition(menu);
				}
				array.add(o);
				param.put("${"+menu.getConfigKey()+"}", o.getJSONArray("conditionEnum")
						.getJSONObject(0).getString("value"));
			}
			re.put("CONDITION_LIST", array);
		}
		
		// 获取top select
		if( selectTopMenu!=null ){
			JSONArray a = getTopSelect(selectTopMenu, param);
			re.put("SELECT_TOP_LIST", a);
			param.put("${"+selectTopMenu.getConfigKey()+"}", a.getJSONObject(0).getString("id"));
		}
		
		// 获取图表数据
		if( otherMenuList.size()>0 ){
			JSONArray arr = new JSONArray();
			JSONObject obj;
			for( AppMenu menu : otherMenuList ){
				obj = new JSONObject();
				if( menu.getConfigType().equalsIgnoreCase("DATA_CHART") ){
					obj.put("type", "CHART");
					obj.put("data", getChartData(menu, param));
					obj.put("title", menu.getMenuName());
				}else if( menu.getConfigType().equalsIgnoreCase("DATA_TABLE") ){
					obj.put("type", "TABLE");
					obj.put("data", getTableData(menu, param));
					obj.put("title", menu.getMenuName());
				}else{
					continue;
				}
				arr.add(obj);
			}
			re.put("DATA_LIST", arr);
		}
		
		return re.toJSONString();
	}
	
	@Override
	public String getReportDetail(Long parentId, Map<String, String> param) {
		JSONObject re = new JSONObject();
		
		AppMenu selectTopMenu = null;
		List<AppMenu> conditionMenuList = new ArrayList<AppMenu>(), 
				otherMenuList = new ArrayList<AppMenu>();
		
		List<AppMenu> list = appMenuAtomSV.queryAppMenus(parentId);
		for( AppMenu menu : list ){
			switch( menu.getConfigType() ) {
				case "SELECT_TOP_LIST" : {
					selectTopMenu = menu;
					break;
				}
				case "CONDITION" : {
					conditionMenuList.add(menu);
					break;
				}
				case "CONDITION_SQL" : {
					conditionMenuList.add(menu);
					break;
				}
				default :{
					otherMenuList.add(menu);
					break;
				}
			}
		}
		
		// 获取top select
		if( selectTopMenu!=null ){
			JSONArray a = getTopSelect(selectTopMenu, param);
			re.put("SELECT_TOP_LIST", a);
		}
		
		// 获取图表数据
		if( otherMenuList.size()>0 ){
			JSONArray arr = new JSONArray();
			JSONObject obj;
			for( AppMenu menu : otherMenuList ){
				obj = new JSONObject();
				if( menu.getConfigType().equalsIgnoreCase("DATA_CHART") ){
					obj.put("type", "CHART");
					obj.put("data", getChartData(menu, param));
					obj.put("title", menu.getMenuName());
				}else if( menu.getConfigType().equalsIgnoreCase("DATA_TABLE") ){
					obj.put("type", "TABLE");
					obj.put("data", getTableData(menu, param));
					obj.put("title", menu.getMenuName());
				}else{
					continue;
				}
				arr.add(obj);
			}
			re.put("DATA_LIST", arr);
		}
		
		return re.toJSONString();
	}
	
	/**
	 * 获取日期范围
	 * @param menu
	 * @return
	 */
	private JSONObject getDate(AppMenu menu){
		JSONObject object = new JSONObject();
		List<List<String>> data = GetDataUtil.getDataBySqlKey(menu);
		if( data.size()==1 ){
			List<String> row = data.get(0);
			object.put("minDate", row.get(0));
			object.put("maxDate", row.get(1));
			object.put("currentDate", row.get(1));
		}
		return object;
	}
	
	/**
	 * 获取条件 通过sql
	 * @param menu
	 * @return
	 */
	private JSONObject getConditionSql(AppMenu menu){
		JSONObject object = new JSONObject();
		List<List<String>> data = GetDataUtil.getDataBySqlKey(menu);
		object.put("conditionName", menu.getMenuName());
		
		JSONArray conditionArray = new JSONArray();
		JSONObject condition;
		for( List<String> row : data ){
			condition = new JSONObject();
			condition.put("column", menu.getConfigKey());
			if( row.size()==2 ){
				condition.put("value", row.get(0));
				condition.put("name", row.get(1));
			}else if( row.size()==1 ){
				condition.put("value", row.get(0));
				condition.put("name", row.get(0));
			}else{
				continue;
			}
			conditionArray.add(condition);
		}
		object.put("conditionEnum", conditionArray);
		return object;
	}
	
	/**
	 * 获取条件 通过title
	 * @param menu
	 * @return
	 */
	private JSONObject getCondition(AppMenu menu){
		JSONObject object = new JSONObject();
		List<List<String>> data = GetDataUtil.getDataBySqlKey(menu);
		object.put("conditionName", menu.getMenuName());
		
		JSONArray conditionArray = new JSONArray();
		JSONObject condition;
		if( data.size()==1 ){
			for( String cell : data.get(0) ){
				condition = new JSONObject();
				condition.put("column", menu.getConfigKey());
				String[] cells = cell.split("[|]");
				if( cells.length==2 ){
					condition.put("value", cells[0]);
					condition.put("name", cells[1]);
				}else if( cells.length==1 ){
					condition.put("value", cells[0].trim());
					condition.put("name", cells[0].trim());
				}else{
					continue;
				}
				conditionArray.add(condition);
			}			
		}
		object.put("conditionEnum", conditionArray);
		return object;
	}

	/**
	 * 获取顶部指标滑动数据
	 * @param menu
	 * @param param
	 * @return
	 * 
	 */
	private JSONArray getTopSelect(AppMenu menu, Map<String, String> param){
		JSONArray array = new JSONArray();
		List<List<String>> data = GetDataUtil.getDataBySqlKey(menu, param);
		
		String check = null;
		String key = "${"+menu.getConfigKey()+"}";
		if( param.containsKey(key) ){
			check = param.get(key);
		}
		
		if( data.size()==4 ){
			int i = 0; JSONObject o;
			for( String name : data.get(0) ){
				o = new JSONObject();
				o.put("name", name);
				o.put("column", menu.getConfigKey());
				o.put("id", data.get(1).get(i));
				o.put("ableClick", new Boolean(data.get(2).get(i).trim()));
				o.put("value", data.get(3).get(i));
				if( check==null ){
					o.put("isCheck", i==0?true:false);
				}else{
					if( check.equalsIgnoreCase(data.get(1).get(i)) ){
						o.put("isCheck", true);
					}else{
						o.put("isCheck", false);
					}
				}
				array.add(o);
				i++;
			}
		}
		return array;
	}
	
	/**
	 * 获取图形数据
	 * @param menu
	 * @param param
	 * @return
	 */
	private JSONObject getChartData(AppMenu menu, Map<String, String> param){
		JSONObject json = new JSONObject();
		
		List<List<String>> list = GetDataUtil.getDataBySqlKey(menu, param);
		
		if( list!=null&&list.size()>0 ){
			// 月份
			List<String> first = list.remove(0);
			
			first.remove(0);
			json.put("xAxis", first); // x轴
			json.put("data", new JSONArray());
			json.put("tooltipData", new JSONArray());
			
			JSONArray barArr = new JSONArray();
			JSONArray lineArr = new JSONArray();
			
			for(List<String> row : list){
				// 1:柱,2:折线,3:悬浮展示
				String key = row.remove(0);
				String keys[] = key.split("[|]");
				if( keys.length!=3 ) continue;
				
				JSONObject d = new JSONObject();
				d.put("name", keys[0]);
				d.put("data", row);
				if( keys[1].equals("1") ){
					d.put("type", "bar");
					d.put("unit", keys[2]);
				}else if( keys[1].equals("2") ){
					d.put("type", "line");
					d.put("yAxisIndex", "1");
					d.put("unit", keys[2]);
				}else if( keys[1].equals("3") ){
					d.put("unit", keys[2]);
				}
				
				if( keys[1].equals("3") ){
					json.getJSONArray("tooltipData").add(d);
				}else if( keys[1].equals("1") ){
					barArr.add(d);
				}else{
					lineArr.add(d);
				}
			}
			
			json.getJSONArray("data").addAll(barArr);
			json.getJSONArray("data").addAll(lineArr);
		}
		return json;
	}
	
	/**
	 * 获取表格数据
	 * @param menu
	 * @param param
	 * @return
	 */
	private JSONArray getTableData(AppMenu menu, Map<String, String> param) {
		JSONArray array = new JSONArray();
		List<List<String>> list = GetDataUtil.getDataBySqlKey(menu, param);
		if ( list!=null&&list.size()>0 ) {
			array = JSONArray.parseArray(JSONArray.toJSONString(list));
		}
		
		return array;
	}
	
}
